package com.seantone.inappupdate.core;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.seantone.inappupdate.core.define.CheckUpdateParams;
import com.seantone.inappupdate.core.impl.ICheckUpdateCallBack;
import com.seantone.inappupdate.core.impl.IInAppUpdate;
import com.seantone.inappupdate.core.impl.IRequestUpdate;
import com.seantone.inappupdate.core.utils.ClassUtils;
import com.seantone.inappupdate.plugin.PluginProguard;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class InAppUpdate {
    private List<IInAppUpdate> mShareSDKMap = new ArrayList<IInAppUpdate>();
    private static InAppUpdate mInstace = null;

    private IInAppUpdate selectInAppUpdate = null;

    private InAppUpdate(){

    }

    public static InAppUpdate getInstance() {
        if (null == mInstace) {
            mInstace = new InAppUpdate();
        }
        return mInstace;
    }

    IInAppUpdate getBestOrSupper(Context context){
        List<IInAppUpdate> supportList = new ArrayList<IInAppUpdate>();
        for(int i = 0; i < mShareSDKMap.size(); ++i){
           if(mShareSDKMap.get(i).isBest(context)){
               return mShareSDKMap.get(i);
           }
            if(mShareSDKMap.get(i).isSupport(context)){
                supportList.add(mShareSDKMap.get(i));
            }
        }

        if(supportList.size() > 0){
            Random rand = new Random(System.currentTimeMillis());
            return mShareSDKMap.get(rand.nextInt(supportList.size()));
        }
        return null;
    }

    public  void checkUpdate(Context context, String paramsStr, ICheckUpdateCallBack callBack){
        CheckUpdateParams params = new Gson().fromJson(paramsStr, CheckUpdateParams.class);
        checkUpdate( context, params,  callBack);
    }

    public void checkUpdate(Context context, CheckUpdateParams params, ICheckUpdateCallBack callBack){
        if(selectInAppUpdate != null){
            selectInAppUpdate.checkUpdate(context, params, callBack);
        }
    }
    public  void requestUpdate(Context context, IRequestUpdate callBack){
        if(selectInAppUpdate != null){
            selectInAppUpdate.requestUpdate(context, callBack);
        }
    }

    public void init(Context context) {
        Log.i("xsdk", "init with context");
        Package pack = PluginProguard.class.getPackage();
        String packageName = pack.getName();

        List<Class> allClass = null;
        try {
            allClass = ClassUtils.getClasses(context, packageName);
        } catch (Exception e) {
            Log.e("xsdk", e.getMessage());
            e.printStackTrace();
            return;
        }
        for (int i = 0; i < allClass.size(); i++) {
            Class clazz = allClass.get(i);
            if (IInAppUpdate.class.isAssignableFrom(clazz)) {
                Object sdk = null;
                try {
                    sdk = clazz.newInstance();
                } catch (Exception e) {
                    e.printStackTrace();
                    continue;
                }
                mShareSDKMap.add((IInAppUpdate) sdk);
            }
        }
        selectInAppUpdate =  getBestOrSupper(context);
    }
}
